﻿using DeviceCommon;
using LogLib;
using Newtonsoft.Json.Linq;
using System;
using System.IO;

namespace SignatureBoard
{
    public class SignatureBoard : DeviceBase
    {
        private string _licence = "";

        private SignatureBoardOption _option = new SignatureBoardOption();

        public SignatureBoard()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            _option.Load();
        }

        public override bool DoAction(string param, out string outParam)
        {
            Log.WriteAsync("进入个性化");
            outParam = "";
            //try
            //{
            //    JObject inParam = JObject.Parse(param);
            //    string fileName = (string)inParam["ImageFile"];
            //    string tipsContent = (string)inParam["TipsContent"];
            //    string tipsTitle = (string)inParam["TipsTitle"];
            //    string errMsg = "";
            //    WacomSignatureBoard signatrueBoard = new WacomSignatureBoard();
            //    if (signatrueBoard.DoSignature(_licence, tipsContent, tipsTitle, fileName, out errMsg))
            //    {
            //        return true;
            //    }
            //    else
            //    {
            //        _error = "签名失败:" + errMsg;
            //        Log.WriteAsync(_error);
            //        return false;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    _error = "签名失败:" + ex.ToString();
            //    Log.WriteAsync(ex.ToString());
            //    return false;
            //}

            return true;
        }

        public override void Exit(string inParam, out string outParam)
        {
            Log.WriteAsync("Exit。。。");
            outParam = "";
        }

        public override void Init(string inParam, out string outParam)
        {
            Log.WriteAsync("Start Init");
            outParam = "";
            _licence = _option.Licence;
            if (string.IsNullOrEmpty(_licence))
            {
                _error = string.Format("签名版初始化失败，授权码为空");
                Log.WriteAsync(_error);
                _bInited = false;                
            }
            else
            {
                Log.WriteAsync(string.Format("签名版初始化完成，授权码：{0}", _licence));
                _bInited = true;        
            }
            Log.WriteAsync("End Init");            
        }

        public override void ShowConfigDialog(string inParam)
        {
           
        }

        protected override void OnWorkingCompleted(string outParams)
        {
            base.OnWorkingCompleted(outParams);
        }      

        protected override void OnWorkingFailed(string message)
        {
            base.OnWorkingFailed(message);
        }

        public override void Start(string inParam, out string outParam)
        {
            Log.WriteAsync("Start。。。");
            outParam = "";
            try
            {
                Log.WriteAsync(inParam);             
                JObject inParamJobj = JObject.Parse(inParam);
                string fileName = (string)inParamJobj["JsonObj"]["ImageFile"];
                string tipsContent = (string)inParamJobj["JsonObj"]["TipsContent"];
                string tipsTitle = (string)inParamJobj["JsonObj"]["TipsTitle"];
                string errMsg = "";
                WacomSignatureBoard signatrueBoard = new WacomSignatureBoard();
                if (signatrueBoard.DoSignature(_licence, tipsContent, tipsTitle, fileName, out errMsg))
                {
                    outParam = "签名成功";
                    Log.WriteAsync(outParam);
                    OnWorkingCompleted(inParam);
                }
                else
                {
                    _error = "签名失败:" + errMsg;                  
                    Log.WriteAsync(_error);
              
                    OnWorkingFailed("");
                }
            }
            catch (Exception ex)
            {
                _error = "签名失败:" + ex.ToString();
                Log.WriteAsync(ex.ToString());           
                OnWorkingFailed("");
            }
        }

        public override void Stop(string inParam, out string outParam)
        {
            Log.WriteAsync("Stop。。。");
            outParam = "";
        }
    }
}
